<?php
/**
 * 基础模型类
 * Created by PhpStorm.
 * Author: Lin
 * Date: 2017/12/20
 * Time: 10:33
 */

namespace app\base;

use think\Model;

class BaseModel extends Model
{
    protected $connection = '';//数据库链接信息
    protected $table = '';//表名
//    protected $pk = 'id';//主键

    public function __construct($data = [])
    {
        parent::__construct($data);
    }

    protected function baseWhere($select, $where)
    {
        if (is_array($where) && ! empty($where)) {
            foreach ($where as $key => $value) {
                if (is_array($value)) {
                    foreach ($value as $k => $v) {
                        if ($k == '>=') {
                            $select->where($key, $k, $v);
                        } else if ($k == '<=') {
                            $select->where($key, $k, $v);
                        } else if ($k == 'in') {
                            $select->whereIn($key, $v);
                        } else if ($k == 'notIn') {
                            $select->whereNotIn($key, $v);
                        } else if ($k == 'like') {
                            $select->whereLike($key, $v);
                        }
                    }
                } else {
                    $select->where($key, '=', $value);
                }
            }
        }
        return $select;
    }

    protected function baseJoin($select, $join)
    {
        if ($join) {
            /**
             * join查询 joinTable->关联的表名 condition->关联条件 type->连接类型,默认为inner join
             * $join = ['joinTable' => 'role r', 'condition' => 'role_perm.role_id = r.id', 'type' => 'inner'];
             *
             * 多个连表查询操作
             * $join[] = [];
             * $join[] = [];
             */
            foreach ($join as $key => $value) {
                if (is_array($value)) {
                    if (isset($value['joinTable']) && isset($value['condition'])) {
                        $type = isset($value['type']) && $value['type'] ? strtoupper($value['type']) : 'INNER';
                        $select->join($value['joinTable'], $value['condition'], $type);
                    }
                } else {
                    $select->join($join['joinTable'], $join['condiction']);
                    break;
                }
            }
        }
        return $select;
    }

    /**
     * 获取数据列表
     * @param array $where 查询条件
     * @param string $field 查询字段
     * @param string $orderBy 排序
     * @param string $groupBy 分组
     * @param array $join 连接查询
     * @param string $limit 查询数据条数
     * @param string $offset 查询数据起始位置
     * @param string $alias 数据表别名
     * @param bool $getSql 是否返回SQL
     * @return BaseModel|false|\PDOStatement|string|\think\Collection
     */
    public function getList($where = [], $field = '', $orderBy = '', $groupBy = '', $join = [], $limit = '', $offset = '', $alias = '', $getSql = false)
    {
        $select = $this;
        if ($field) $select->field($field);
        if ($alias) $select->alias($alias);
        $select = $this->baseWhere($select, $where);

        /*$join[] = ['joinTable' => 'permission p', 'condition' => 'role_perm.perm_id = p.id', 'type' => 'inner'];
        $join[] = ['joinTable' => 'role r', 'condition' => 'role_perm.role_id = r.id', 'type' => 'inner'];*/
        $select = $this->baseJoin($select, $join);

        if ($limit || $offset) {
            $select->limit(($limit?:0), ($offset?:0));
        }
        if ($orderBy) $select->order($orderBy);
        if ($groupBy) $select->group($groupBy);
        if ($getSql) return $select->fetchSql(true)->select();
        $select = $select->select();
        return $select;
    }

    /**
     * 获取数据并分页
     * @param array $where 查询条件
     * @param $field 查询字段
     * @param null $limit 查询数据条数
     * @param null $orderBy 排序
     * @param null $groupBy 分组
     * @param string $alias 数据表别名
     * @param array $join 连表查询
     * @param bool $getSql 是否返回SQL
     * @return $this|BaseModel|false|\PDOStatement|string|\think\Collection|\think\Paginator
     */
    public function getListForPage($where = [], $field = null, $limit = null, $orderBy = null, $groupBy = null, $alias = '', $join = [], $getSql = false)
    {
        $select = $this;
        if ($field) $select->field($field);//查询的字段
        if ($alias) $select->alias($alias);//数据表别名
        $select = $this->baseWhere($select, $where);

        /*$join[] = ['joinTable' => 'permission p', 'condition' => 'role_perm.perm_id = p.id', 'type' => 'inner'];
        $join[] = ['joinTable' => 'role r', 'condition' => 'role_perm.role_id = r.id', 'type' => 'inner'];*/

        $select = $this->baseJoin($select, $join);
        if (! $limit) $limit = 30;//默认取多少条数据
        if ($orderBy) $select->order($orderBy);//排序
        if ($groupBy) $select->group($groupBy);//分组
        if ($getSql) return $select->fetchSql(true)->select();//返回SQL语句
        $select = $select->paginate($limit);//分页
        return $select;
    }
}